package com.b1502.store2.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;

import com.b1502.store2.R;
import com.b1502.store2.activity.MyOrderListActivity;
import com.b1502.store2.adapter.CategoryItemAdapter;
import com.b1502.store2.model.CategoryItem;
import com.b1502.store2.network.StoreParams;
import com.b1502.store2.util.UrlUtil;
import com.google.gson.Gson;

import org.xutils.common.Callback;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

public class CategoryFragment extends BaseFragment {

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    
    private List<CategoryItem> mList;
    private String mParam1;
    private String mParam2;
    private View mView;
    private GridView mCatagort_grid;
    private CategoryItemAdapter mCategoryItemAdapter;
    private CategoryItem[] mCategoryItem;

    // TODO: Rename and change types and number of parameters
    public static CategoryFragment newInstance(String param1, String param2) {
        CategoryFragment fragment = new CategoryFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.fragment_category,container,false);
    
        return mView;
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mCatagort_grid = (GridView) mView.findViewById(R.id.catagort_grid);
        getItems();

        mCategoryItemAdapter = new CategoryItemAdapter(getActivity());
        
        mCatagort_grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent=new Intent(getActivity(), MyOrderListActivity.class);
                intent.putExtra("name",mCategoryItem[position].getName());
                intent.putExtra("id",mCategoryItem[position].getId());
                startActivityForResult(intent,1);
            }
        });
        
    }

        private void getItems() {
             StoreParams params = new StoreParams(UrlUtil.GetCategories);
            x.http().get(params, new Callback.CommonCallback<String>() {

                

                @Override
              public void onCancelled(CancelledException cex) {

              }
                
                @Override
              public void onSuccess(String result) {
                    Log.d(TAG, result);
                    mList=new ArrayList<CategoryItem>();
                    Gson gson=new Gson();
                    mCategoryItem = gson.fromJson(result, CategoryItem[].class);

                    
                
                    mCategoryItemAdapter.setData(mCategoryItem);
                    mCatagort_grid.setAdapter(mCategoryItemAdapter);
                    
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

}
